'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 定时送礼
 * @author sundbcn@gmail.com (sun debing)
 */

var _ = require('lodash');
var async = require('async');
var moment = require('moment');
var Code = require('../../../consts/code');
var moduleType = require('../../../consts/moduleType');
var operationType = require('../../../consts/operationType');
var PlayerManager = require('../../../libs/playerManager');
var onlineConf = require('../../../config/onlineConf');
var rewardService = require('../../../services/rewardService');
var genRewardService = require('../../../services/genRewardService');
var constantUtil = require('../../../util/constantUtil.js');

module.exports = function (app) {
    return new Handler(app);
};

var Handler = function (app) {
    this.app = app;
};

/**
 * 领取"定时送礼"奖励
 */
Handler.prototype.reward = function (msg, session, next) {
    var role_id = session.uid;

    var player_manager = new PlayerManager(role_id);
    var reward = null;
    var is_crit = 0;
    var this_online_conf = null;
    var this_reward_id;
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(
                    ['role', 'online', 'activity', 'arena', 'expedition', 'bag', 'build', 'task', 'achieve', 'daily_task', 'tech', 'accrued'],
                    cb
                );
            },
            function (cb) {
                // 条件判断
                var role_model = player_manager.role;
                var online_model = player_manager.online;

                // 在线时长判断
                if (moment().unix() < online_model.getNextRewardTime()) {
                    return next(null, {
                        code: Code.ACTIVITY_NOT_ENOUGH_ONLINE_TIME
                    });
                }

                // 判断是否已领取所有奖励
                this_reward_id = online_model.getNextRewardId();
                if (this_reward_id === 0) {
                    return next(null, {
                        code: Code.ACTIVITY_ALL_REWARD_HAVE_RECEIVE
                    });
                }
                this_online_conf = onlineConf.getOnlineConf(this_reward_id);
                if (!this_online_conf) {
                    return next(null, {
                        code: Code.ACTIVITY_ALL_REWARD_HAVE_RECEIVE
                    });
                }

                cb();
            },
            function (cb) {
                // 设置下一次奖励，发放奖励
                var online_model = player_manager.online;

                // 设置下一次可以领取的时间
                var next_online_conf = onlineConf.getNextOnlineConf(this_reward_id);
                if (!!next_online_conf) {
                    var next_reward_id = next_online_conf.id;
                    online_model.setNextRewardId(next_reward_id); //设置下次奖励ID
                    online_model.setNextRewardTime(moment().unix() + next_online_conf.id); //设置下次在线到期时间
                } else {
                    online_model.setNextRewardId(0);
                    online_model.setNextRewardTime(2147483647);
                }

                // 奖励暴击翻倍处理
                var random = _.random(0, 100);
                if (random <= constantUtil.value('online_crit_probability')) {
                    is_crit = 1;
                }

                //发放本次在线奖励
                reward = genRewardService.generateReward(this_online_conf);
                if (!reward) {
                    return next(null, {
                        code: Code.REWARD_ERROR
                    });
                }

                var num = 1;
                if (!!is_crit) {
                    num = constantUtil.value('online_crit_num');
                }

                for (var i = 0; i < num; i++) {
                    rewardService.addRewardToRole(player_manager, reward, moduleType.MDL_ACT_ONLINE_TIME, operationType.OPR_ACT_ONLINE_TIME_REWARD);
                }

                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK,
                result: {
                    reward: [reward],
                    is_crit: is_crit
                }
            });
        }
    );
};
